package com.focusai.efairy.ui.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.focusai.efairy.R;
import com.focusai.efairy.model.manager.CacheManager;
import com.focusai.efairy.model.user.User;
import com.focusai.efairy.network.Image.ImageLoaderFactory;
import com.focusai.efairy.ui.activity.AboutActivity;
import com.focusai.efairy.ui.activity.SettingActivity;
import com.focusai.efairy.ui.activity.more.UserDetailInfoActivity;
import com.focusai.efairy.ui.base.BaseFragment;

/**
 * Created by luo_guangwen on 2017/7/11.
 */

public class MoreFragment extends BaseFragment implements View.OnClickListener {
    private static final int REQUESTCODE_USER_DETAIL = 0x100;
    private ImageView imgAvater;
    private TextView tvUserName;
    private TextView tvUserRole;

    public MoreFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_more, container, false);
        initView(view);
        setDefaultValues();
        return view;
    }

    @Override
    protected void setDefaultValues() {
        initAvaterOrName();
    }

    @Override
    protected void initView(View view) {
        findView(view, R.id.more_set_app_ll).setOnClickListener(this);
        findView(view, R.id.more_about_app_ll).setOnClickListener(this);
        findView(view, R.id.more_user_ll).setOnClickListener(this);
        imgAvater = findView(view, R.id.more_avater_iv);
        tvUserName = findView(view, R.id.more_name_tv);
        tvUserRole = findView(view, R.id.more_name_role);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.more_about_app_ll:
                Intent intent = new Intent(getContext(), AboutActivity.class);
                startActivity(intent);

                break;
            case R.id.more_set_app_ll:
                Intent setIntent = new Intent(getContext(), SettingActivity.class);
                startActivityForResult(setIntent, SettingActivity.REQUESTCODE_SETTING);

                break;
            case R.id.more_user_ll:
                Intent userIntent = new Intent(getContext(), UserDetailInfoActivity.class);
                startActivityForResult(userIntent, REQUESTCODE_USER_DETAIL);
                break;
            default:
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == SettingActivity.REQUESTCODE_SETTING) {
                getActivity().finish();
            } else if (requestCode == REQUESTCODE_USER_DETAIL) {
                initAvaterOrName();
            }
        }
    }

    private void initAvaterOrName() {
        ImageLoaderFactory.getImageLoader().loadImage(getContext(), imgAvater, CacheManager.getUserAvater());
        tvUserName.setText(CacheManager.getUserName());
        int role = CacheManager.getUserRoleLevel(); // 0-超级管理员 1-一级经销商 2-二级经销商 3-项目管理员 4-普通用户
        tvUserRole.setText(User.getRoleName(role));
    }
}
